//
//  CustomerViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/14.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "CustomerViewController.h"
#import "Customermodel.h"
#import "MJRefresh.h"
#import "MyCustomerTableViewCell.h"

@interface CustomerViewController ()
{
    int page;
    
}
@end

@implementation CustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"我的客户" fontSize:18];
    
    page=1;
    
    [self customTableView];
    
    
    [self addLeftBackBtn:nil];
    
    
    UINib *nib=[UIView loadNibNamed:@"MyCustomerTableViewCell" bundle:nil];
    
    [self.tableview registerNib:nib forCellReuseIdentifier:@"MyCustomerTableViewCell"];
    
    __weak typeof(self)weakSelf=self;
    [weakSelf requestData];
    
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        page=1;
        [weakSelf requestData];
        
    }];
    
    
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        page++;
        [weakSelf requestData];
        
    }];
    
    
    

    // Do any additional setup after loading the view from its nib.
}
-(void)requestData
{
    
    
    [self addProgressHUD];
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId], @"artisanId",[NSString stringWithFormat:@"%d",page], @"page", nil];
    
    NSLog(@"订单详情参数：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/order/getUserOrderRanking" parameters:dictionary option:RequestCenterCachePolicyCacheAndLocal sucess:^(id responseObject) {
        [self.tableview.mj_header endRefreshing];
        
        [self.tableview.mj_footer endRefreshing];
        
        
        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            NSArray *array=responseObject[@"orderList"];
            
            if (array.count<1) {
                page--;

            }
            for (NSDictionary *dic in array) {
                Customermodel *model=[[Customermodel alloc]initWithDic:dic];
                [self.dataArray addObject:model];
                
                
            }
            
            
        }
        else{
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        [self.tableview reloadData];
        
        
    } failur:^(NSError *error) {
        page--;
        
        [self removeStatuslable];
        [self.tableview.mj_header endRefreshing];
        
        [self.tableview.mj_footer endRefreshing];
        
        
    }];
 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    __weak typeof(self)weakSelf=self;
    //
    //    MessageModel *model=self.dataArray[indexPath.row];
    
    MyCustomerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomerTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
            [cell cellFuzhi:self.dataArray[indexPath.row]];
    
    
    return cell;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
